package com.unisound.tracelog.client.core.autoconfigure;

import cn.hutool.core.lang.generator.SnowflakeGenerator;
import com.unisound.tracelog.client.core.listener.KafkaTraceLogListener;
import com.unisound.tracelog.client.core.listener.LogTraceLogListener;
import com.unisound.tracelog.client.core.listener.TraceLogListeners;
import com.unisound.tracelog.client.core.properties.TraceLogClientProperties;
import com.unisound.tracelog.client.core.service.TraceLogService;
import com.unisound.tracelog.client.core.service.impl.TraceLogServiceImpl;
import com.unisound.tracelog.client.core.util.SpringUtil;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.KafkaTemplate;

@Configuration
@EnableConfigurationProperties({TraceLogClientProperties.class})
public class TraceLogClientCoreAutoConfiguration {

    private KafkaTemplate<String, String> kafkaTemplate;

    private TraceLogClientProperties traceLogClientProperties;

    public TraceLogClientCoreAutoConfiguration(KafkaTemplate<String, String> kafkaTemplate, TraceLogClientProperties traceLogClientProperties) {
        this.kafkaTemplate = kafkaTemplate;
        this.traceLogClientProperties = traceLogClientProperties;
    }

    @Bean
    public TraceLogService traceLogService() {
        return new TraceLogServiceImpl(traceLogClientProperties, new SnowflakeGenerator(1, 0), traceLogListeners());
    }

    @Bean
    public TraceLogListeners traceLogListeners() {
        TraceLogListeners traceLogListeners = new TraceLogListeners();
        traceLogListeners.register(new KafkaTraceLogListener(kafkaTemplate, traceLogClientProperties));
        traceLogListeners.register(new LogTraceLogListener(traceLogClientProperties));
        return traceLogListeners;
    }

    @Bean
    public SpringUtil traceLogSpringUtil() {
        return new SpringUtil();
    }

}
